var params = {
  pageNumber:1,
  pageSize:10
};

var firstPage = true;
var lastPage = false;
var totalPage = 0;




/*
自定义模板规则：商机信息列表
*/
Handlebars.registerHelper('oppList', function(items, options) {
  var itemArr = new Array();
  if(items instanceof Array){
    itemArr = items;
  }else if(items instanceof Object){
    itemArr.push(items);
  } 
  var out = '<table class="am-table am-text-sm">'
        +'<thead>'
        +'<tr>'
        +'<th class="am-text-center">预受理编码</th>'
        +'<th class="am-text-center">预受理名称</th>'
        +'<th class="am-text-center">业务大类</th>'
        +'<th class="am-text-center">业务小类</th>'
        +'</tr>'
        +'</thead>'
        +'<tbody class="am-text-center">';
  for(var i=0, l=itemArr.length; i<l; i++) {
    //var item = options.fn(items[i]);
    var item = itemArr[i];
    //details[item.id]=item;
    out += "<tr onclick='click2details(\""+item.PRE_BUSI_ID+"\")'>"
        +'<td>'+item.PRE_BUSI_ID+'</td>'
        +'<td>'+item.PRE_BUSI_NAME+'</td>'
        +'<td>'+item.BUSI_TYPE_NAME+'</td>'
        +'<td>'+item.BUSI_CLASS_NAME+'</td>'
        +'</tr>';
  }

  out += '</tbody></table>';

  return out;
});

/**
封装新增按钮和搜索条数据
*/
var getAddAndSearchObj = function(){
  var obj = {};
  obj.addBtnId = "add";
  obj.searchBtnId = "search";
  obj.searchPlaceholder = "查询";

  return obj;
}

var click2details=function(detailsId){
  var url =  "opportunity_details.html" + "?";
  var parameters = getParams();
  if(parameters !=""){
    url += parameters + "&";
  }
  url += "id="+detailsId;
  window.location.href= url;
};

var showDatas = function(datas){
  if(!datas || $.isEmptyObject(datas)){
    showLoadingBar($("#loadingbar"),true,true,"暂无要获取的信息！");
    return;
  }
  showLoadingBar($("#loadingbar"),false);
  var $tpl = $('#info-tpl');
  var source = $tpl.text();
  var template = Handlebars.compile(source);
  var myData = {
    list: datas
  };
  var html = template(myData);
  $("#info").html(html);
}


var pageBtn = function(option){
  if(option=="prev"){
    if(!firstPage){
      params.pageNumber -= 1;
    }else{
      alert("已经是第一页啦");
      return false;
    }
  }else if(option=="next"){
    if(!lastPage){
      params.pageNumber += 1;
    }else{
      alert("已经是最后一页啦");
      return false;
    }
  }else if(option=="first"){
    params.pageNumber = 1;
  }else if(option=="last"){
  params.pageNumber = totalPage;
  }
  loadDatas();

}
var setPage_text = function(){
  var page_text = "第"+params.pageNumber+"页/共"+totalPage+"页";
  $("#page_text").html(page_text);
}


var loadDatas = function(){
  $("#info").html("");
  showLoadingBar($("#loadingbar"),true,false,"加载中，请稍候……");
  var reqUrl = "../../../opportunityH5/getOpportunityList";
  $.ajax({
    type: "post",
    data: params,
    url: reqUrl,
    dataType: "json",
    async: true,
    headers: {
      'token': getParamValue('token'),
      'saleType': getParamValue('saleType')
    },
    success: function(data) {
      if (data.ResultCode === 1009) {
        alert('登陆超时，请重新登录！');
        jumpToSignin();
        return;
      }
      firstPage = data.firstPage;
      lastPage = data.lastPage;
      totalPage = data.totalPage;
      if(totalPage==0){
        $("#pager_div").addClass("hidden_pager");
      }else if(totalPage>0){
        $("#pager_div").removeClass("hidden_pager");
      }
      var returnData = data.list;
      showDatas(returnData);
      setPage_text();
    },
    error : function(textStatus) {
      showLoadingBar($("#"+loadingBarId),true,true,"网络或服务器异常，请稍后再试！");
      console.log("request error:");
    }
  });
};

$(function(){
  //显示新增按钮+搜索条
  showItem(getAddAndSearchObj(),"add-search-tpl");
  //加载商机列表信息
  loadDatas();

  $("#add").on("click",function(){
    var jump_url = "opportunity.add.html";
    if(getParams() && getParams()!=""){
      jump_url +="?"+getParams();
    }
    window.location.href = jump_url;
  });
  
  $("#search").on("click",function(){
    //显示搜索条件弹窗
    $('#my-prompt').modal({
      relatedTarget: this,
      onConfirm: function(e) {
        params.PRE_BUSI_ID = $("#opp_id").val();
        params.PRE_BUSI_NAME = $("#opp_name").val();
        params.pageNumber = 1;
        loadDatas();
      }
    });
  });
});